//
//  ZZSettingController.m
//  NetesaeLottery
//
//  Created by 周英培 on 16/8/2.
//  Copyright © 2016年 周英培. All rights reserved.
//

#import "ZZSettingController.h"
#import "ZZBlueController.h"
#import "ZZSettingCell.h"
@interface ZZSettingController ()

@property (nonatomic,strong) NSArray *groupArr;

@end

@implementation ZZSettingController

/// 返回分组样式
- (instancetype)init {

    return [super initWithStyle:UITableViewStyleGrouped];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.groupArr.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSDictionary *dict = self.groupArr[section];
    
    NSArray *items = dict[@"HMItems"];
    
    return items.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *dict = self.groupArr[indexPath.section];
    
    NSArray *items = dict[@"HMItems"];
    
    NSDictionary *item = items[indexPath.row];
    
    ZZSettingCell *cell = [ZZSettingCell cellWithTableView:tableView andItem:item];
    
    cell.cellItem = item;
    
    return cell;

}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSDictionary *dict = self.groupArr[section];
    
    return dict[@"HMHeader"];
    
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    NSDictionary *dict = self.groupArr[section];
    
    return dict[@"HMFooter"];

}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.groupArr[indexPath.section];
    
    NSArray *items = dict[@"HMItems"];
    
    NSDictionary *item = items[indexPath.row];
    
    //MARK:根据字符串，转化为SE了类型，实现检查新版本的功能的实现
    NSString *funcStr = item[@"HMCallFunc"];
    
    if (funcStr.length > 0 ) {
        
        SEL func = NSSelectorFromString(funcStr);
        
        if ([self respondsToSelector:func]) {
            
            [self performSelector:func];
            
        }
        
    }
    
    //MARK:根据字符串，转化为OC类型,进行控制器跳转的选择
    NSString *classStr = item[@"HMTargetVc"];
    
    if (classStr.length <= 0) {
        
        return;
        
    }
    
    Class className = NSClassFromString(classStr);
    
    // 多态
    UIViewController *vc = [[className alloc] init];
    
    vc.navigationItem.title = item[@"HMTitle"];
    
    if ([vc isKindOfClass:[ZZSettingController class]]) {
        
        ZZSettingController *setVC = (ZZSettingController *)vc;
        
        setVC.plistName = item[@"HMPlistName"];
        
        [self.navigationController pushViewController:setVC animated:YES];
        
    } else {
    
        [self.navigationController pushViewController:vc animated:YES];
    
    }
    
    

}

- (void)checkUpdate {

    UIAlertController *alVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经是最新版本" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alVC addAction:done];
    
    [self presentViewController:alVC animated:YES completion:nil];

}



#pragma mark - 懒加载
- (NSArray *)groupArr {

    if (!_groupArr) {
        
        _groupArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.plistName ofType:@"plist"]];
        
    }
    
    return _groupArr;
}
@end
